import { nextTick } from 'vue'
import { useAppStore } from '@/store/modules/app'

export function useTabs() {
  const appStore = useAppStore()

  const tabsEdit = (title, component, src, currentName, params) => {
    let tabPage = { title, component, src, params }
    appStore.removeTabPages(component)
    nextTick(() => {
      appStore.addTabPages(tabPage)
      if (currentName) {
        appStore.removeTabPages(currentName)
      }
    })
  }

  const tabsRemove = (name) => {
    appStore.removeTabPages(name)
  }

  const tabsAdd = (title, component, src, params) => {
    let tabPage = { title, component, src, file: component, params }
    appStore.addTabPages(tabPage)
  }

  const tabsAddRepeat = (title, component, src, params) => {
    let tabPage = {
      title,
      component: component + Math.random(),
      src,
      file: component,
      params,
    }
    appStore.addTabPagesRepeat(tabPage)
  }

  return {
    tabsEdit,
    tabsRemove,
    tabsAdd,
    tabsAddRepeat,
  }
}
